package handlers

import (
	"car-contract-backend/models"
	"github.com/gin-gonic/gin"
	"strconv"
)

func AddWarehouse(c *gin.Context) {
	ware_name := c.PostForm("ware_name")
	ware_addr := c.PostForm("ware_addr")
	ware_types := c.PostForm("ware_types")
	if ware_name == "" || ware_addr == "" || ware_types == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Warehouse{
		WareName:  ware_name,
		WareAddr:  ware_addr,
		WareTypes: ware_types,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func AddOrder(c *gin.Context) {
	order_name := c.PostForm("order_name")
	order_num, _ := strconv.Atoi(c.PostForm("order_num"))
	wid, _ := strconv.Atoi(c.PostForm("wid"))
	if order_name == "" || order_num == 0 || wid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderName: order_name,
		OrderNum:  order_num,
		Wid:       wid,
	}
	err := models.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
